package com.gitee.fsmxm.ocon.util;

import com.gitee.fsmxm.ocon.constant.MsoEncoding;
import com.gitee.fsmxm.ocon.constant.WdDocumentDirection;
import com.gitee.fsmxm.ocon.constant.WdOpenFormat;
import com.gitee.fsmxm.ocon.constant.WdSaveFormat;

import java.util.Locale;

import static com.gitee.fsmxm.ocon.constant.WdSaveFormat.*;

/**
 * @author fsmxm
 */
public class WordUtils {

    private WordUtils() {
    }

    /**
     * params of open
     *
     * @param fileName 文件名
     * @return params of open
     * @see WordUtils#paramOfOpen
     */
    public static Object[] paramOfOpen(String fileName) {
        return paramOfOpen(
                fileName,
                false,
                true,
                false,
                null,
                null,
                false,
                null,
                null,
                null,
                null,
                true,
                true,
                null,
                true,
                null);
    }

    /**
     * params of open
     *
     * @param fileName              Required. The name of the document (paths are accepted).
     * @param confirmConversions    Optional. True to display the Convert File dialog box if the file is not in Microsoft Word format.
     * @param readOnly              Optional. True to open the document as read-only.
     *                              This argument does not override the read-only recommended setting on a saved document.
     *                              For example, if a document has been saved with read-only recommended turned on,
     *                              setting the ReadOnly argument to False will not cause the file to be opened as read/write.
     * @param addToRecentFiles      Optional. True to add the file name to the list of recently used files at the bottom of the File menu.
     * @param passwordDocument      Optional. The password for opening the document.
     * @param passwordTemplate      Optional. The password for opening the template.
     * @param revert                Optional. Controls what happens if FileName is the name of an open document.
     *                              True to discard any unsaved changes to the open document and reopen the file.
     *                              False to activate the open document.
     * @param writePasswordDocument Optional. The password for saving changes to the document.
     * @param writePasswordTemplate Optional. The password for saving changes to the template.
     * @param format                Optional. The file converter to be used to open the document.
     *                              Can be one of the WdOpenFormat constants.
     *                              The default is {@link WdOpenFormat#WD_OPEN_FORMAT_AUTO}.
     * @param encoding              Optional. The document encoding (code page or character set) to be used by Word
     *                              when you view the saved document.
     *                              Can be any valid {@link MsoEncoding} constant.
     *                              For the list of valid MsoEncoding constants, see the Object Browser in the Visual Basic Editor.
     *                              The default is the system code page.
     * @param visible               Optional. True if the document is opened in a visible window. The default is True.
     * @param openAndRepair         Optional. True to repair the document to prevent document corruption.
     * @param documentDirection     Optional. Indicates the horizontal flow of text in a document.
     *                              Can be any valid WdDocumentDirection constant.
     *                              The default is {@link WdDocumentDirection#WD_LEFT_TO_RIGHT}.
     * @param noEncodingDialog      Optional. True to skip displaying the Encoding dialog box that Word displays
     *                              if the text encoding cannot be recognized. The default is False.
     * @param xmlTransform          Optional. Specifies a transform to use.
     * @return params of open
     * @see <a href="https://docs.microsoft.com/en-us/office/vba/api/word.documents.opennorepairdialog">word.documents.opennorepairdialog</a>
     */
    @SuppressWarnings("all")
    public static Object[] paramOfOpen(String fileName,
                                       Boolean confirmConversions,
                                       Boolean readOnly,
                                       Boolean addToRecentFiles,
                                       String passwordDocument,
                                       String passwordTemplate,
                                       Boolean revert,
                                       String writePasswordDocument,
                                       String writePasswordTemplate,
                                       Integer format,
                                       Integer encoding,
                                       Boolean visible,
                                       Boolean openAndRepair,
                                       Integer documentDirection,
                                       Boolean noEncodingDialog,
                                       Object xmlTransform) {
        return new Object[]{
                VT.get(fileName),
                VT.get(confirmConversions),
                VT.get(readOnly),
                VT.get(addToRecentFiles),
                VT.get(passwordDocument),
                VT.get(passwordTemplate),
                VT.get(revert),
                VT.get(writePasswordDocument),
                VT.get(writePasswordTemplate),
                VT.get(format),
                VT.get(encoding),
                VT.get(visible),
                VT.get(openAndRepair),
                VT.get(documentDirection),
                VT.get(noEncodingDialog),
                VT.get(xmlTransform)
        };
    }

    public static Object[] paramOfSaveAs(String fileName, Integer fileFormat) {
        return paramOfSaveAs(
                fileName,
                fileFormat,
                null,
                null,
                false,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null);
    }

    /**
     * param of saveAs
     *
     * @param fileName                The name for the document. The default is the current folder and file name.
     *                                If the document has never been saved, the default name is used (for example,
     *                                Doc1.doc). If a document with the specified file name already exists, the
     *                                document is overwritten without prompting the user.
     * @param fileFormat              The format in which the document is saved. Can be any {@link WdSaveFormat} constant.
     *                                To save a document in another format, specify the appropriate value for the
     *                                SaveFormat property of the FileConverter object.
     * @param lockComments            True to lock the document for comments. The default is False.
     * @param password                A password string for opening the document. (See Remarks below.)
     * @param addToRecentFiles        True to add the document to the list of recently used files on the File menu.
     *                                The default is True.
     * @param writePassword           A password string for saving changes to the document. (See Remarks below.)
     * @param readOnlyRecommended     True to have Microsoft Word suggest read-only status whenever the document
     *                                is opened. The default is False.
     * @param embedTrueTypeFonts      True to save TrueType fonts with the document. If omitted, the EmbedTrue-
     *                                -TypeFonts argument assumes the value of the EmbedTrueTypeFonts property.
     * @param saveNativePictureFormat If graphics were imported from another platform (for example, Macintosh),
     *                                True to save only the Microsoft Windows version of the imported graphics.
     * @param saveFormsData           True to save the data entered by a user in a form as a record.
     * @param saveAsAOCELetter        If the document has an attached mailer, True to save the document as an
     *                                AOCE letter (the mailer is saved).
     * @param encoding                The code page, or character set, to use for documents saved as encoded text
     *                                files. The default is the system code page. You cannot use all {@link MsoEncoding}
     *                                constants with this parameter.
     * @param insertLineBreaks        If the document is saved as a text file, True to insert line breaks at the
     *                                end of each line of text.
     * @param allowSubstitutions      If the document is saved as a text file, True allows Word to replace some
     *                                symbols with text that looks similar. For example, displaying the copyright
     *                                symbol as (c). The default is False.
     * @param lineEnding              The way Word marks the line and paragraph breaks in documents saved as text
     *                                files. Can be one of the following WdLineEndingType constants:
     *                                wdCRLF (default) or wdCROnly.
     * @param addBiDiMarks            True adds control characters to the output file to preserve bi-directional
     *                                layout of the text in the original document.
     * @return param of saveAs
     */
    @SuppressWarnings("all")
    public static Object[] paramOfSaveAs(String fileName,
                                         Integer fileFormat,
                                         Boolean lockComments,
                                         String password,
                                         Boolean addToRecentFiles,
                                         String writePassword,
                                         Boolean readOnlyRecommended,
                                         Boolean embedTrueTypeFonts,
                                         Boolean saveNativePictureFormat,
                                         Boolean saveFormsData,
                                         Boolean saveAsAOCELetter,
                                         Integer encoding,
                                         Boolean insertLineBreaks,
                                         Boolean allowSubstitutions,
                                         Integer lineEnding,
                                         Boolean addBiDiMarks) {
        return new Object[]{
                VT.get(fileName),
                VT.get(fileFormat),
                VT.get(lockComments),
                VT.get(password),
                VT.get(addToRecentFiles),
                VT.get(writePassword),
                VT.get(readOnlyRecommended),
                VT.get(embedTrueTypeFonts),
                VT.get(saveNativePictureFormat),
                VT.get(saveFormsData),
                VT.get(saveAsAOCELetter),
                VT.get(encoding),
                VT.get(insertLineBreaks),
                VT.get(allowSubstitutions),
                VT.get(lineEnding),
                VT.get(addBiDiMarks)
        };
    }

    public static int getFormatType(String targetExt) {
        switch (targetExt.toLowerCase(Locale.ENGLISH)) {
            case "doc":
                return WD_FORMAT_DOCUMENT97;
            case "docx":
                return WD_FORMAT_XML_DOCUMENT;
            case "html":
            case "htm":
                return WD_FORMAT_HTML;
            case "mhtml":
            case "mht":
                return WD_FORMAT_WEB_ARCHIVE;
            case "pdf":
                return WD_FORMAT_PDF;
            case "rtf":
                return WD_FORMAT_RTF;
            case "txt":
                return WD_FORMAT_TEXT;
            case "xml":
                return WD_FORMAT_XML;
            default:
                return -1;
        }
    }

}
